﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using XBS.Resources;
using XBS.Converter;
using XBS.Model;

using System.ComponentModel;

namespace XBS
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            DataContext = App.ViewModel;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            StoriesListOnPage.ListStories.SelectionChanged += ListStories_SelectionChanged;
            RecentStoriesLLS.ListRecentStories.SelectionChanged += ListRecentStories_SelectionChanged;
            BookmarkListOnPage.ListBookmark.SelectionChanged += ListBookmark_SelectionChanged;
        }

        void ListRecentStories_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                Story story = (Story)e.AddedItems[0];
                if (story == null)
                    return;

                Uri storyPage = new Uri("/StoryPage.xaml?story=" + story.Index, UriKind.Relative);

                ((LongListSelector)sender).SelectedItem = null;

                NavigationService.Navigate(storyPage);
            }
        }

        void ListStories_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                Story story = (Story)e.AddedItems[0];
                if (story == null)
                    return;

                Uri storyPage = new Uri("/StoryPage.xaml?story=" + story.Index, UriKind.Relative);

                ((LongListSelector)sender).SelectedItem = null;

                NavigationService.Navigate(storyPage);
            }
        }

        void ListBookmark_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                Bookmark bookmark = (Bookmark)e.AddedItems[0];
                if (bookmark == null)
                {
                    return;
                }

                Uri storyPage = new Uri("/StoryPage.xaml?story=" + bookmark.StoryIndex + "&paragraph=" + bookmark.ParagraphIndex, UriKind.Relative);
                ((LongListSelector)sender).SelectedItem = null;
                NavigationService.Navigate(storyPage);
            }
        }

        private void SettingBtn_Click(object sender, EventArgs e)
        {
            Uri settingPage = new Uri("/SettingPage.xaml", UriKind.Relative);
            NavigationService.Navigate(settingPage);
        }

        private void AboutBtn_Click(object sender, EventArgs e)
        {
            Uri aboutPage = new Uri("/AboutPage.xaml", UriKind.Relative);
            NavigationService.Navigate(aboutPage);
        }
    }
}